package com.xmb.wechat.view.wechat.chat;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.bean.GroupTalkBean;
import com.xmb.wechat.bean.WechatContactBean;
import com.xmb.wechat.lsn.SendMsgCenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class WechatChatAddCallActivity extends BaseActivity {
    @BindView(R2.id.tv_sender_name)
    TextView       tvSenderName;
    @BindView(R2.id.iv_avatar)
    ImageView      ivAvatar;
    @BindView(R2.id.iv_arrow)
    ImageView      ivArrow;
    @BindView(R2.id.btn_pick_sender)
    RelativeLayout btnPickSender;
    @BindView(R2.id.rv)
    RecyclerView   rv;
    @BindView(R2.id.tv_name)
    TextView       tvName;
    @BindView(R2.id.btn_save)
    Button         btnSave;
    @BindView(R2.id.tv_title)
    TextView       tvTitle;
    @BindView(R2.id.spinner_min)
    Spinner        spinnerMin;
    @BindView(R2.id.spinner_second)
    Spinner        spinnerSecond;

    private WechatContactBean mInitWechatContact, sender;
    private GroupTalkBean           mInitGroupTalk;
    private boolean                 isRoomChat;
    private ContactAdapter          adapter;
    private List<WechatContactBean> list;
    private boolean                 isVideoCall;

    public WechatChatAddCallActivity() {
        super(R.layout.wechat_activity_chat_send_voice_video_call);
    }

    public static void start(Activity act, WechatContactBean contactBean, boolean isVideoCall) {
        Intent it = new Intent(act, WechatChatAddCallActivity.class);
        it.putExtra("isRoomChat", false);
        it.putExtra("WechatContactBean", contactBean);
        it.putExtra("isVideoCall", isVideoCall);
        act.startActivity(it);
    }

    public static void start(Activity act, GroupTalkBean groupTalkBean, boolean isVideoCall) {
        Intent it = new Intent(act, WechatChatAddCallActivity.class);
        it.putExtra("isRoomChat", true);
        it.putExtra("GroupTalkBean", groupTalkBean);
        it.putExtra("isVideoCall", isVideoCall);
        act.startActivity(it);
    }

    @Override
    protected void initView() {
        isVideoCall = getIntent().getBooleanExtra("isVideoCall", false);
        tvTitle.setText(isVideoCall ? "视频通话" : "语音通话");

        list = new ArrayList<>();
        //添加数据（me）
        WechatContactBean me = WechatContactBean.instanceNewContactMe(this);
        onSenderChange(me);
        list.add(me);
        //添加数据（other）
        isRoomChat = getIntent().getBooleanExtra("isRoomChat", false);
        if (!isRoomChat) {
            mInitWechatContact = (WechatContactBean) getIntent().getSerializableExtra("WechatContactBean");
            list.add(mInitWechatContact);
        } else {//群聊
            mInitGroupTalk = (GroupTalkBean) getIntent().getSerializableExtra("GroupTalkBean");
            list.addAll(mInitGroupTalk.getGroupMembersList());
        }
        //发送者选择adapter
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        rv.setLayoutManager(layoutManager);
        adapter = new ContactAdapter(R.layout.wechat_select_sender_contact_item, list);
        rv.setAdapter(adapter);
    }

    @OnClick({R2.id.btn_pick_sender, R2.id.btn_save})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.btn_pick_sender) {
            if (rv.getVisibility() == View.VISIBLE) {
                rv.setVisibility(View.GONE);
            } else {
                rv.setVisibility(View.VISIBLE);
            }
        } else if (i == R.id.btn_save) {
            onClickSend();
        }
    }

    private void onClickSend() {
        //检查时长：
        String minStr = (String) spinnerMin.getSelectedItem();
        String secondStr = (String) spinnerSecond.getSelectedItem();
        int min = Integer.parseInt(minStr.replace("分", ""));
        int second = Integer.parseInt(secondStr.replace("秒", ""));
        if (second == 0 && min == 0) {
            ToastUtils.showShort("通话时长不能为0");
            return;
        }
        String content = "";
        if (second < 10) {
            content = "0" + second;
        } else {
            content = second + "";
        }
        if (min < 10) {
            content = "0" + min + ":" + content;
        } else {
            content = min + ":" + content;
        }
        SendMsgCenter.sendCall(new SendMsgCenter.SendMsgListener() {
            @Override
            public void onSendSuc() {
                finish();
            }

            @Override
            public void onSendError(String errorMsg) {

            }
        }, sender.getId(), isRoomChat, isRoomChat ? mInitGroupTalk.getId() : mInitWechatContact.getId(), isVideoCall, content);

    }

    public class ContactAdapter extends BaseQuickAdapter<WechatContactBean, BaseViewHolder> {
        public ContactAdapter(int layoutResId, List data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, final WechatContactBean vo) {
            helper.setText(R.id.name, vo.getName());
            WechatContactBean.setupAvatarIntoImageView(vo,
                    (ImageView) helper.getView(R.id.iv_avatar), 12);
            helper.setChecked(R.id.rb, vo.isSelected());
            helper.getView(R.id.vg).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (WechatContactBean item : ContactAdapter.this.getData()) {
                        item.setSelected(false);
                    }
                    notifyDataSetChanged();
                    onSenderChange(vo);
                }
            });
        }
    }

    private void onSenderChange(WechatContactBean vo) {
        vo.setSelected(true);
        sender = vo;
        rv.setVisibility(View.GONE);
        WechatContactBean.setupAvatarIntoImageView(vo, ivAvatar, 12);
        tvSenderName.setText(vo.getName());
    }
}
